package test;
import java.util.Scanner;
public class Formation {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = scanner.nextInt();
        }

        scanner.close();
        int[] perm = new int[n];
        for (int i = 0; i < n; i++) {
            perm[i] = i + 1;//初始化排列

        }
        int count = 0;
        do {
            if (isValid(perm,a)) {
                count++;
            }
        }while (Permutation(perm));
        System.out.println(count);
    }

    private static boolean Permutation(int[] perm) {
        int k = perm.length -2 ;
        while (k>= 0 && perm[k] >= perm[k + 1]){
            k--;
        }
        if (k < 0) {
            return false;
        }
        int l = perm.length - 1;
        while (perm[l] <= perm[k]){

            l--;
        }
        int temp = perm[k];
        perm[k] = perm[l];
        perm[l] = temp;
        //反转
        int left = k + 1,right = perm.length - 1;
        while(left < right){
            temp = perm[left];
            perm[left] = perm[right];
            perm[right] = temp;
            left++;
            right--;
        }
        return true;
    }

    private static boolean isValid(int[] perm, int[] a) {
        for (int i = 0; i < perm.length; i++) {
            if (a[perm[i] - 1] != perm[i] && a[perm[i] - 1] != perm[i] + 1) {
                return false;
            }
        }
        return true;
    }


}
